//
//  Entity.cpp
//  kasama1
//
//  Created by 丁逸鹏 on 15/3/3.
//
//

#include "Entity.h"
Entity::Entity()
{
    
}

void Entity::onEnter()
{
    Node::onEnter();
    
    
    scales=0.5;
    int num=global->partriesnum;
    int index=rand()%num;
    int tag=global->randpatries[index];
    this->setTag(tag);
    char spname[30];
    sprintf(spname,"images/Pastries/%d.png",tag);
    sprite=Sprite::create(spname);
    
    sprite->setScale(scales);
    //sprite->setAnchorPoint(Vec2(0,0));
    this->addChild(sprite);
    this->changeState(Entity::normal);
    //创建一个触摸监听
    //auto dispatcher = Director::getInstance()->getEventDispatcher();
    /*auto listener = EventListenerTouchOneByOne::create();
     listener->onTouchBegan = CC_CALLBACK_2(Entity::ccTouchBegan, this);
     
     dispatcher->addEventListenerWithSceneGraphPriority(listener, this);*/
}
//bool Entity::ccTouchBegan(CCTouch* touch, CCEvent* event)
//{
//
//	//CCLOG("%f,%f,%f,%f",touch->getLocation().x,touch->getLocation().y,
//		//this->getContentSize().width,sprite->getContentSize().width);
//	if(this->getBoundingBox().containsPoint( touch->getLocation()))
//	{
//		this->runAction(JumpBy::create(0.5,Vec2(0,0),5,1));
//		CCLOG("row:%d,col:%d",this->getRow(),this->getCol());
//		return true;
//	}
//    return false;
//
//}
Rect Entity::getBoundingBox()
{
    return Rect(this->getPositionX()-sprite->getContentSize().width/2*scales,
                this->getPositionY()-sprite->getContentSize().height/2*scales,
                sprite->getContentSize().width*scales,
                sprite->getContentSize().height*scales);
}
void Entity::setXY(int xx,int yy)
{
    x=xx;
    y=yy;
}
void Entity::changeState(State state)
{
    this->sprite->stopAllActions();
    this->setNowState(state);
    switch (this->getNowState())
    {
        case normal:
            //this->stopAllActions();
            break;
        case rowcrush:
            this->sprite->runAction(RepeatForever::create(Sequence::create(MoveBy::create(0.2,Vec2(2,0)),
                                                                           MoveBy::create(0.2,Vec2(-2,0)),NULL)));
            break;
        case colcrush:
            this->sprite->runAction(RepeatForever::create(Sequence::create(MoveBy::create(0.2,Vec2(0,2)),
                                                                           MoveBy::create(0.2,Vec2(0,-2)),NULL)));
            break;
        case boss:
            this->sprite->runAction(RepeatForever::create(RotateBy::create(0.2,Vec3(0,0,30))));
            break;
        default:
            break;
    }
    
}